//
//  GradientRingView.m
//  WCGradientLayerExample
//
//  Created by 陈晓强 on 2017/5/6.
//  Copyright © 2017年 huangwenchen. All rights reserved.
//

#import "GradientRingView.h"
#import "GradientCircleLayer.h"
#import "UICountingLabel.h"

@interface GradientRingView ()

@property (strong, nonatomic) GradientCircleLayer *circleLayer;
@property (strong, nonatomic) GradientCircleLayer *backLayer;
@property (strong, nonatomic) UICountingLabel *midLabel;
@property (strong, nonatomic) UILabel *topLabel;
@property (strong, nonatomic) UILabel *botLabel;
@property (assign, nonatomic) CGFloat value;
@property (assign, nonatomic) CGFloat maxValue;
@property (strong, nonatomic) UIColor *fromColor;
@property (strong, nonatomic) UIColor *toColor;


@end

@implementation GradientRingView

- (UILabel *)labelWithFrame:(CGRect)frame text:(NSString *)text font:(CGFloat)size color:(UIColor *)color {
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = color;
    label.font = [UIFont systemFontOfSize:[self valueWithOrigin:size]];
    label.text = text;
    return label;
}

- (instancetype)initWithFrame:(CGRect)rect fromColor:(UIColor *)fromColor to:(UIColor *)toColor topText:(NSString *)top midText:(NSString *)mid botText:(NSString *)botText value:(CGFloat)value maxValue:(CGFloat)maxValue{
    if (self = [super initWithFrame:rect]) {
        self.value = value;
        self.maxValue = maxValue;
        self.fromColor = fromColor;
        self.toColor = toColor;
        if (top) {
            self.topLabel = [self labelWithFrame:CGRectMake(0, 0, rect.size.width, [self valueWithOrigin:18]) text:top font:[self valueWithOrigin:16] color:[UIColor colorWithWhite:1 alpha:1]];
            [self addSubview:self.topLabel];
        }

        CGFloat circleY = top ? CGRectGetMaxY(self.topLabel.frame) + [self valueWithOrigin:2] : 0;
        CGFloat circleWidth = rect.size.height - circleY;
        CGFloat circleX = (rect.size.width - circleWidth)/2;
        self.circleLayer = [[GradientCircleLayer alloc] initWithFrame:CGRectMake(circleX, circleY, circleWidth, circleWidth) fromColor:fromColor to:toColor lineWidth:[self valueWithOrigin:8]];
        self.backLayer = [[GradientCircleLayer alloc] initWithFrame:CGRectMake(circleX, circleY, circleWidth, circleWidth) fromColor:[UIColor colorWithWhite:1 alpha:.1] to:[UIColor colorWithWhite:1 alpha:.1] lineWidth:[self valueWithOrigin:8]];
        [self.backLayer addProgress:1 animated:NO];
        [self.layer addSublayer:self.backLayer];
        [self.layer addSublayer:self.circleLayer];
        
        self.midLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, circleY + self.circleLayer.bounds.size.width / 2 - [self valueWithOrigin:23] / 2, rect.size.width, [self valueWithOrigin:20])];
        self.midLabel.textColor = [UIColor colorWithWhite:1 alpha:1];
        self.midLabel.font = [UIFont systemFontOfSize:[self valueWithOrigin:20]];
        self.midLabel.text = mid;
        self.midLabel.textAlignment = NSTextAlignmentCenter;
        self.midLabel.format = @"%ld";
        
        [self.midLabel setFormatBlock:^NSString *(CGFloat value) {
            return @((int)value).stringValue;
        }];
        
        self.botLabel = [self labelWithFrame:CGRectMake(0, CGRectGetMaxY(self.midLabel.frame) , rect.size.width, [self valueWithOrigin:18]) text:botText font:[self valueWithOrigin:13] color:[UIColor colorWithWhite:1 alpha:0.9]];
        
        [self addSubview:self.midLabel];
        [self addSubview:self.topLabel];
        [self addSubview:self.botLabel];
    }
    
    return self;
}
- (void)animateToValue:(CGFloat)value {
    CGFloat valueOld = self.value;
    [self.midLabel countFrom:valueOld to:value withDuration:1];
    self.value = value;
//    self.midLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)value];
    [self.circleLayer addAnimationWithProgress:MIN(value / self.maxValue, 1)];
}

- (CGFloat)valueWithOrigin:(CGFloat)originValue {
    if (self.frame.size.width < 99) {
        return floor(originValue * 0.7);
    } else {
        return originValue;
    }
}

- (void)configFrame:(CGRect)frame {
    self.frame = frame;
    [self updateFrame];
}

- (void)updateFrame {
    CGRect rect = self.frame;
    if (self.topLabel) {
        [self.topLabel removeFromSuperview];
        self.topLabel = [self labelWithFrame:CGRectMake(0, 0, rect.size.width, [self valueWithOrigin:18]) text:self.topLabel.text font:[self valueWithOrigin:16] color:[UIColor colorWithWhite:1 alpha:0.9]];
        [self addSubview:self.topLabel];
    }
        
    CGFloat circleY = self.topLabel ? CGRectGetMaxY(self.topLabel.frame) + [self valueWithOrigin:8] : 0;
    CGFloat circleWidth = rect.size.height - circleY;
    CGFloat circleX = (rect.size.width - circleWidth)/2;
    GradientCircleLayer *circleLayer = [[GradientCircleLayer alloc] initWithFrame:CGRectMake(circleX, circleY, circleWidth, circleWidth) fromColor:self.fromColor to:self.toColor lineWidth:[self valueWithOrigin:25]];
    GradientCircleLayer *backLayer = [[GradientCircleLayer alloc] initWithFrame:CGRectMake(circleX, circleY, circleWidth, circleWidth) fromColor:[UIColor colorWithWhite:1 alpha:.1] to:[UIColor colorWithWhite:1 alpha:0.1] lineWidth:[self valueWithOrigin:25]];
    [self.backLayer addProgress:1 animated:NO];
    [self.layer replaceSublayer:self.backLayer with:backLayer];
    [self.layer replaceSublayer:self.circleLayer with:circleLayer];
    
    UICountingLabel *midLabel = [[UICountingLabel alloc] initWithFrame:CGRectMake(0, CGRectGetMidY(self.circleLayer.bounds) - [self valueWithOrigin:23] / 2, rect.size.width, [self valueWithOrigin:23])];
    midLabel.textColor = [UIColor colorWithWhite:1 alpha:0.9];
    midLabel.font = [UIFont systemFontOfSize:[self valueWithOrigin:23]];
    midLabel.text = self.midLabel.text;
    midLabel.textAlignment = NSTextAlignmentCenter;
    midLabel.format = @"%ld";
    
    [midLabel setFormatBlock:^NSString *(CGFloat value) {
        return @((int)value).stringValue;
    }];
    
    [self.midLabel removeFromSuperview];
    self.midLabel = midLabel;
    [self addSubview:self.midLabel];
    
    [self.botLabel removeFromSuperview];
    self.botLabel = [self labelWithFrame:CGRectMake(0, CGRectGetMaxY(self.midLabel.frame) , rect.size.width, [self valueWithOrigin:20]) text:self.botLabel.text font:[self valueWithOrigin:14] color:[UIColor colorWithWhite:1 alpha:0.9]];
    [self addSubview:self.botLabel];

}
@end
